package com.guli.gulimall.gulimallware.service.impl;

import com.guli.common.utils.R;
import com.guli.gulimall.gulimallproduct.entity.SkuInfoEntity;
import com.guli.gulimall.gulimallware.Feign.ProductFeign;
import com.guli.gulimall.gulimallware.entity.WareInfoEntity;
import com.guli.to.SkuHasStockVo;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.Query;

import com.guli.gulimall.gulimallware.dao.WareSkuDao;
import com.guli.gulimall.gulimallware.entity.WareSkuEntity;
import com.guli.gulimall.gulimallware.service.WareSkuService;
import org.springframework.transaction.annotation.Transactional;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {
    @Autowired
    private WareSkuDao wareSkuDao;
    @Autowired
    private ProductFeign productFeign;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<WareSkuEntity> wareInfoEntityQueryWrapper = new QueryWrapper<>();

        String skuId = (String) params.get("skuId");
        if(null!=skuId && !skuId.isEmpty() && !"0".equalsIgnoreCase(skuId)){
            wareInfoEntityQueryWrapper.eq("sku_id",skuId);

        }
        String wareId = (String) params.get("wareId");
        if(null!=wareId && !wareId.isEmpty() && !"0".equalsIgnoreCase(wareId)){
            wareInfoEntityQueryWrapper.eq("ware_id",wareId);

        }
        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params), wareInfoEntityQueryWrapper);

        return new PageUtils(page);
    }
    @Transactional
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //1判断如果没有这个库存记录，则新增
        List<WareSkuEntity> list = this.list(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if(list.size()>0){
            wareSkuDao.addStock( skuId,  wareId,  skuNum);
        }else{
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //如果远程调用失败，无需回滚
            try {
                R info = productFeign.info(skuId);
                Map<String,Object> data = (Map<String, Object>) info.get("skuInfo");
                System.out.println((String) data.get("skuName"));
                if(info.getCode()==0){
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            }catch (Exception E){
                System.out.println(1);
            }

            wareSkuDao.insert(wareSkuEntity);
        }

    }

    @Override
    public List<SkuHasStockVo> hashStock(List<Long> sku_ids) {
        System.out.println(sku_ids);
        List<SkuHasStockVo> collect = sku_ids.stream().map(item -> {
            SkuHasStockVo skuHasStockVo = new SkuHasStockVo();
            List<WareSkuEntity> wareSkuEntityList = this.baseMapper.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", item));
            int countS = 0;
            int countLS = 0;
            if (null != wareSkuEntityList && wareSkuEntityList.size() > 0) {
                for (int i = 0; i < wareSkuEntityList.size(); i++) {
                    countS += wareSkuEntityList.get(i).getStock();
                    countLS += wareSkuEntityList.get(i).getStockLocked();
                }
            }
            /**
             * 有些库存已经下单，但是没支付，所以要减去锁定的
             */
            if (countS - countLS > 0) {
                skuHasStockVo.setHashStock(true);
                skuHasStockVo.setStock(countS);

            } else {
                skuHasStockVo.setHashStock(false);
                skuHasStockVo.setStock(0);
            }
            skuHasStockVo.setStockLocked(countLS);
            skuHasStockVo.setSkuId(item);
            return skuHasStockVo;
        }).collect(Collectors.toList());
        System.out.println(collect);
        return collect;
    }

}